#include "StdAfx.h"
#include "HistogramObj.h"
#include "WordObj.h"


CHistogramObj::CHistogramObj(void):CActiveComponentObj()
{
	m_ActiveComponentType=ACTIVE_COMPONENT_HISTOGRAM;
	m_dblDriveParam=0.0;
	m_dblRange=100;
	m_dblMidRange=m_dblRange*0.5;
	m_dblTotalWidth=0.0;
	m_eHistogramType=No_Histogram_Type;
	m_dblStartRange=0.0;
}

CHistogramObj::CHistogramObj( CEntityObj* pParentEnt ):CActiveComponentObj(pParentEnt)
{
	m_ActiveComponentType=ACTIVE_COMPONENT_HISTOGRAM;
	m_dblDriveParam=0;
	m_dblRange=100;
	m_dblMidRange=m_dblRange*0.5;
	m_dblTotalWidth=0.0;
	m_eHistogramType=No_Histogram_Type;
	m_dblStartRange=0.0;
}

CHistogramObj::CHistogramObj( CHistogramObj* pHistogramObj ):CActiveComponentObj(pHistogramObj)
{
	m_ActiveComponentType=pHistogramObj->GetActiveComponentType();
	m_dblDriveParam=pHistogramObj->GetDriveParam();
	m_dblRange=pHistogramObj->GetRange();
	m_dblMidRange=pHistogramObj->GetMidRange();
	m_dblTotalWidth=pHistogramObj->GetTotalWidth();
	m_eHistogramType=pHistogramObj->GetHistogramType();
	m_dblStartRange=pHistogramObj->GetStartRange();
}

CHistogramObj::~CHistogramObj(void)
{

}
void CHistogramObj::Serialize( CArchive & ar )
{
	if (ar.IsStoring())
	{
		//ar << m_dblDriveParam;
		ar << m_dblRange;
		ar << m_dblMidRange;
		ar << m_dblTotalWidth;
		if (CObj::m_lFileVersion>74)
		{
			ar << (int)m_eHistogramType;
			ar <<m_dblStartRange;
		}		
	}
	else
	{
		//ar >> m_dblDriveParam;
		ar >> m_dblRange;
		ar >> m_dblMidRange;
		ar >> m_dblTotalWidth;
		if (CObj::m_lFileVersion>74)
		{
			int nType(0);
			ar >> nType;
			m_eHistogramType=(eHistogramType)nType;
			ar >>m_dblStartRange;
		}		
	}
	CActiveComponentObj::Serialize(ar);
}

CDisplayObj * CHistogramObj::CreateDisplayObj( long lLayerId )
{
	CHistogramDisObj *pHistogramDisObj = new CHistogramDisObj(this);
	pHistogramDisObj->SetLayerId(lLayerId);

	list<CShape *>::iterator it_shape;
	for (it_shape=m_ShapeList.begin(); it_shape!=m_ShapeList.end(); it_shape++)
	{
		(*it_shape)->CreateDisplayObj(lLayerId);
	}

	return pHistogramDisObj;
}

void CHistogramObj::ModifyWordShapeTxt(double dblDriveParam)
{
	eHistogramType eType=GetHistogramType();
	CWordObj *pMaxWord=NULL;
	CWordObj *pMidWord=NULL;
	CWordObj *pStartWord=NULL;
	list<CShape *>::iterator it_shape;	
	for (it_shape=m_ShapeList.begin(); it_shape!=m_ShapeList.end(); it_shape++)
	{
		CShape *pShape = *it_shape;
		if (pShape->IsWord())
		{
			CWordObj *pWordObj=(CWordObj *)pShape;
			if (pWordObj->GetIsMidRangeTxt())
			{
				pMidWord=pWordObj;
			}
			if (pWordObj->GetIsRangeTxt())
			{
				pMaxWord=pWordObj;
			}
			if (pWordObj->GetIsStartTxt())
			{
				pStartWord=pWordObj;
			}
		}		
	}
	if (eType==Percent_Type)
	{
		pMaxWord->SetText(_T("100%"));
		pMidWord->SetText(_T("50%"));
		pStartWord->SetText(_T("0"));
		SetRange(1);
		SetStartRange(0);
	}
	if (eType==Positive_Type)
	{
		double dblrange=GetRange();
		if (dblDriveParam> dblrange) 
		{
		 	dblrange=(int)(dblDriveParam*1.1);
		 	if ((int)dblrange%2==1)
		 	{
		 		dblrange=dblrange+1;
		 	}
		 	SetRange(dblrange);
			CString szName=_T("");
			szName.Format(_T("%d"),(int)dblrange);
			pMaxWord->SetText(szName);
			szName.Format(_T("%d"),(int)GetMidRange());
			pMidWord->SetText(szName);
		}
	}
	if (eType==PositiveAndNegative_Type)
	{
		double dblrange=GetRange();
		if (dblDriveParam> dblrange) 
		{
			dblrange=(int)(dblDriveParam*1.1);
			if ((int)dblrange%2==1)
			{
				dblrange=dblrange+1;
			}
			SetRange(dblrange);
			CString szName=_T("");
			szName.Format(_T("%d"),(int)dblrange);
			pMaxWord->SetText(szName);
			pMidWord->SetText(_T("0"));
			szName=_T("-")+szName;
			pStartWord->SetText(szName);
		}
	}
}

void CHistogramObj::ModifyRangeTxt(double dblRange)
{
	eHistogramType eType=GetHistogramType();
	CWordObj *pMaxWord=NULL;
	CWordObj *pMidWord=NULL;
	CWordObj *pStartWord=NULL;
	list<CShape *>::iterator it_shape;	
	for (it_shape=m_ShapeList.begin(); it_shape!=m_ShapeList.end(); it_shape++)
	{
		CShape *pShape = *it_shape;
		if (pShape->IsWord())
		{
			CWordObj *pWordObj=(CWordObj *)pShape;
			if (pWordObj->GetIsMidRangeTxt())
			{
				pMidWord=pWordObj;
			}
			if (pWordObj->GetIsRangeTxt())
			{
				pMaxWord=pWordObj;
			}
			if (pWordObj->GetIsStartTxt())
			{
				pStartWord=pWordObj;
			}
		}		
	}
	if (eType==Percent_Type)
	{
		pMaxWord->SetText(_T("100%"));
		pMidWord->SetText(_T("50%"));
		pStartWord->SetText(_T("0"));
		SetRange(1);
		SetStartRange(0);
	}
	if (eType==Positive_Type)
	{

		double dblParam=GetDriveParamData().dblValue;
		if (dblRange < dblParam) 
		{
			dblRange=(int)(dblParam*1.1);
			if ((int)dblRange%2==1)
			{
				dblRange=dblRange+1;
			}
		}
		SetRange(dblRange);
		CString szName=_T("");
		szName.Format(_T("%d"),(int)dblRange);
		pMaxWord->SetText(szName);
		szName.Format(_T("%d"),(int)GetMidRange());
		pMidWord->SetText(szName);		
	}
	if (eType==PositiveAndNegative_Type)
	{
		double dblParam=GetDriveParamData().dblValue;
		if (dblRange < dblParam) 
		{
			dblRange=(int)(dblParam*1.1);
			if ((int)dblRange%2==1)
			{
				dblRange=dblRange+1;
			}
		}
		SetRange(dblRange);

		CString szName=_T("");
		szName.Format(_T("%d"),(int)dblRange);
		pMaxWord->SetText(szName);
		pMidWord->SetText(_T("0"));
		szName=_T("-")+szName;
		pStartWord->SetText(szName);
	}
}
